<?php

namespace App\Http\Controllers\Develop;

use Carbon\Carbon;
use Illuminate\Http\Request;
use App\Models\User\AppVersion;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Redis as Redis;

class AppController extends Controller
{
    public function index()
    {
        return view ('develop/appList');
    }

    //APP版本设置列表
    public function search(Request $request)
    {
        $where = [];

        $appType = $request->input('appType');
        $appVersionCode = $request->input('appVersionCode');

        if (!empty($appType)) {
            $where[] = ['appType',$appType];
        }
        if (!empty($appVersionCode)) {
            $where[] = ['appVersionCode',$appVersionCode];
        }

        $app_data = AppVersion::where($where)
            ->orderBy($request->input('sort'), $request->input('order'))
            ->paginate($request->input('limit'))
            ->toArray();

        //返回数组
        $return = [
            'total' => $app_data['total'],
            'rows' => []
        ];

        if ( $app_data['data'] ) {

            foreach( $app_data['data'] as $row ){

                $operation = '';
                $operation .='<a href="javascript: void(0);" onclick="app.edit(' . $row['appVersionId'] . ',1)" ><span aria-hidden="true" >修改</span></a>
                             <a href="javascript: void(0);" onclick="app.edit('. $row['appVersionId'] .',2)" ><span aria-hidden="true">详情</span></a>';

                if ( $row['appType'] == 1){
                    $appType = 'ios';
                }else{
                    $appType = 'android';
                }

                $return['rows'][] = [
                    'operation' => $operation,
                    'appType' => $appType,
                    'appVersionCode' => $row['appVersionCode'],
                    'appVersionNumber' => $row['appVersionNumber'],
                    'creator' => $row['creator'],
                    'createTime' => $row['createTime']
                ];

            }

        }

        return response()->json($return);

    }

    //添加，修改，详情
    public function edit (Request $request) {

        if ( isset($request['appVersionId']) && !empty($request['appVersionId'])) {
            $AppVersionModel = AppVersion::find($request['appVersionId']);
            if ( !$AppVersionModel) {
                returnJsonData('app版本升级记录没有找到');
            }

            $result = $AppVersionModel;
            $result['flag'] = $_REQUEST['flag'];
            $data = $result['attributes'];
        }else{
            $data = array();
        }

        return view ('develop/appEdit',$data);
    }

    //保存APP版本设置
    public function store(Request $request)
    {

        $appVersionId = $request->input('appVersionId');
        $appVersionCode = $request->input('appVersionCode');
        $appVersionNumber = $request->input('appVersionNumber');
        $apkPath = $request->input('apkPath');
        $appVersionExplain = $request->input('appVersionExplain');

        if ( empty($appVersionCode) ) {
            return response()->json(array(
                'code' => 100001,
                'message' => '版本code不能为空'
            ));
        }
        if ( !isset($appVersionNumber) ) {
            return response()->json(array(
                'code' => 100002,
                'message' => '版本编号不能为空'
            ));
        }
        if ( empty($apkPath) ) {
            return response()->json(array(
                'code' => 100003,
                'message' => '版本下载路径不能为空'
            ));
        }
        if ( empty($appVersionExplain) ) {
            return response()->json(array(
                'code' => 100004,
                'message' => '版本说明不能为空'
            ));
        }
        $app = AppVersion::find($appVersionId);

        if ( !$app ) {
            $app = new AppVersion();
        }
        $app->uuid = makeUuid();
        $app->timeStamp = Carbon::now();
        $app->createTime = Carbon::now();
        $app->creator = Redis::get('WDH_ADMIN_USER_ID_' . session_id());
        $app->appVersionId = $appVersionId;
        $app->appVersionCode = $appVersionCode;
        $app->appType = $request['appType'];
        $app->isMandatoryUpgrade = $request['isMandatoryUpgrade'];
        $app->appVersionNumber = $appVersionNumber;
        $app->apkPath = $apkPath;
        $app->appVersionExplain = $appVersionExplain;

        $app->save();

        return response()->json(['code' => 200, 'message' => '保存成功']);
    }


}